import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { getCategory } from '@/apis/category'
import { onBeforeRouteUpdate } from 'vue-router'


export function useCategory () {
const category = ref({})
const route = useRoute()
const getCategoryDetail = async (id=route.params.id) => {
  const res = await getCategory(id)
  category.value = res.result
}
onMounted(() => {getCategoryDetail()})

//目标：在路由切换时，获取新的分类详情
onBeforeRouteUpdate((to) => {
  getCategoryDetail(to.params.id)
})


return {
  category,
}

}
